package zzs.generalframework.console.core.parameter.commonparameter.operatingpost.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.operatingpost.OperatingPostInfo;
import zzs.generalframework.common.core.parameter.commonparameter.operatingpost.OperatingPostPO;
import zzs.generalframework.console.core.parameter.commonparameter.operatingpost.dao.IOperatingPostDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.operatingpost.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.operatingpost.dao.impl.operatingPostDaoImpl")
public class OperatingPostDaoImpl implements IOperatingPostDao
{
	private Logger logger = LoggerFactory.getLogger(OperatingPostDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(OperatingPostPO operatingPostPO)
	{
		logger.info("====插入工作岗位数据开始====");
		logger.debug("====插入工作岗位数据,入参:OperatingPostPO=[{}]====", operatingPostPO);
		entityManager.persist(operatingPostPO);
		logger.debug("====插入工作岗位数据,出参:boolean=[{}]====", true);
		logger.info("====插入工作岗位数据结束====");
		return true;
	}

	@Override
	public boolean update(OperatingPostPO operatingPostPO)
	{
		logger.info("====更新工作岗位数据开始====");
		logger.debug("====更新工作岗位数据,入参:OperatingPostPO=[{}]====", operatingPostPO);
		entityManager.merge(operatingPostPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新工作岗位数据结束====");
		return true;
	}

	@Override
	public List<OperatingPostPO> select(OperatingPostInfo operatingPostInfo)
	{
		logger.info("====查询工作岗位数据开始====");
		logger.debug("====查询工作岗位数据,入参:OperatingPostInfo=[{}]====", operatingPostInfo);
		List<OperatingPostPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from OperatingPostPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(operatingPostInfo.getOperatingPostID()))
		{
			sqlBuffer.append(" and a.operatingPostID=?");
			parameter.putParameterObject(operatingPostInfo.getOperatingPostID());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostInfo.getOperatingPostCode()))
		{
			sqlBuffer.append(" and a.operatingPostCode=?");
			parameter.putParameterObject(operatingPostInfo.getOperatingPostCode());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostInfo.getOperatingPostName()))
		{
			sqlBuffer.append(" and a.operatingPostName=?");
			parameter.putParameterObject(operatingPostInfo.getOperatingPostName());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostInfo.getOperatingPostDesc()))
		{
			sqlBuffer.append(" and a.operatingPostDesc=?");
			parameter.putParameterObject(operatingPostInfo.getOperatingPostDesc());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(operatingPostInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(operatingPostInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询工作岗位数据,出参:List<OperatingPostPO>=[{}]====", returnList);
		logger.info("====查询工作岗位数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(OperatingPostPO operatingPostPO)
	{
		logger.info("====删除工作岗位数据开始====");
		logger.debug("====删除工作岗位数据,入参:OperatingPostPO=[{}]====", operatingPostPO);
		entityManager.remove(operatingPostPO);
		logger.debug("====删除工作岗位数据,出参:boolean=[{}]====", true);
		logger.info("====删除工作岗位数据结束====");
		return true;
	}

	@Override
	public Long count(OperatingPostPO operatingPostPO)
	{
		logger.info("====统计工作岗位数据开始====");
		logger.debug("====统计工作岗位数据,入参:OperatingPostPO=[{}]====", operatingPostPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from OperatingPostPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(operatingPostPO.getOperatingPostID()))
		{
			sqlBuffer.append(" and a.operatingPostID=?");
			parameter.putParameterObject(operatingPostPO.getOperatingPostID());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostPO.getOperatingPostCode()))
		{
			sqlBuffer.append(" and a.operatingPostCode=?");
			parameter.putParameterObject(operatingPostPO.getOperatingPostCode());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostPO.getOperatingPostName()))
		{
			sqlBuffer.append(" and a.operatingPostName=?");
			parameter.putParameterObject(operatingPostPO.getOperatingPostName());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostPO.getOperatingPostDesc()))
		{
			sqlBuffer.append(" and a.operatingPostDesc=?");
			parameter.putParameterObject(operatingPostPO.getOperatingPostDesc());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(operatingPostPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(operatingPostPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(operatingPostPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计工作岗位数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计工作岗位数据结束====");
		return returnLong;
	}

}
